package uia.alumni.profile;

import java.io.PrintWriter;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.servlet.http.HttpServletRequest;

import javax.transaction.UserTransaction;
import uia.alumni.data.Role;
import uia.alumni.data.User;
import uia.alumni.web.Page;
import uia.alumni.form.*;

import static uia.alumni.form.FieldType.*;
import static uia.alumni.profile.Constants.*;
import static uia.alumni.web.Constants.*;

/**
 * Edit profile for a user. 
 * @Author Simon Zimmermann
 */
public class EditProfile extends Page {

    /** SERVLET_NAME is used for consistency checking of web.xml */
    public static final String SERVLET_NAME = "profile.edit";
    @PersistenceUnit(name = PERSISTENCE_UNIT)
    private EntityManagerFactory emf;
    @Resource
    private UserTransaction tx;

    public EditProfile() {
        super(Role.member);
    }

    public void writeBody(PrintWriter out, HttpServletRequest request) {
        try {
            EntityManager em = emf.createEntityManager();
            User user = getUser(em, request);


            if (user != null) {
                out.println("<h1>Edit profile ("+user.getFullName()+")</h1>");

                // If form has been submitted and had errors. This will show them.
                writeFormErrors(out, request);

                out.println(tagP("This form is used to edit the User Profile details."));
                out.println(tagP(tagA(EditPassword.SERVLET_NAME, "Change password for <em>" + user.getFullName()+ "</em>")));
                out.println("<ul>");
                
                out.println(tagForm(SaveProfile.SERVLET_NAME));

                // If Admin is editing we are allowed to change the user Roles
                if (isAdmin(em, request)) {
                    out.println("<li class=\"adminlist\">");
                    out.println("As admin you have the possiblity to change some user rights." +
                            " Remember, though. Removing admin role from yourself will degrade " +
                            "the user to a normal user. Removing member-role will couse havox. So" +
                            "this is generally discouraged.<br>");
                    for (Role r : Role.values()) {
                        out.println(tagField(r.name(), CHECKBOX, r.name(), user.hasRole(r)));
                    }
                    out.println("</li>");
                }

                // --------------- INPUT: ( name    | type | label      | value )
                out.println(tagLi(tagField(FULLNAME, TEXT, "Full Name:", user.getFullName())));
                out.println(tagLi(tagField(PHONE, TEXT, "Phone number:", user.getPhone())));
                out.println(tagLi(tagField(ADDRESS, TEXT, "Address :", user.getAddress())));
                out.println(tagLi(tagField(MAIL, EMAIL, "Email Address:", user.getEmail())));
                out.println(tagLi(tagField(DESCRIPTION, TEXTAREA, "Description:", user.getDescription())));


                out.println("<input type=\"hidden\" value=" + SERVLET_NAME + " name=\"from\">");
                out.println(tagLi(tagField(SUBMIT.toString(), SUBMIT)));
                out.println("</form>");
                out.println("<ul>");
            }
        } catch (Exception e) {
        } finally {
        }
    }
    public final static long serialVersionUID = 1;
}
